// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 * Creator: yanking
 * Create time: 2022-02-23 23:56
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.leetCode.putFruites;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Predicate;

public class FruitIntoBaskets_ {
    int longest;
    int ct;  // 当前果子的种类
    Set<Integer> treeType = new HashSet<>(); // 储存篮子能装树的果实的种类

    public int totalFruit(int[] fruits) {
        // 求指定最长连续序列
        travel(fruits, 0, 1);
        return longest;
    }

    private void travel(int[] fruits, int index, int l) {
        if (index >= fruits.length - 1) return;// 递归结束标志

        while (index < fruits.length) { // 篮子够装，果树还够看
            if (index == fruits.length - 1 && treeType.contains(fruits[index])) {
                ++l;
                break;
            } else if (index == fruits.length - 1 && !treeType.contains(fruits[index])) {
                break;
            }
            ++index;
            ++l;
        }
        travel(fruits, index, 1); // 继续遍历剩下的果树
        longest = Math.max(longest, l);
    }


    // TO Renew the Set
    public void newSet(Set<Integer> set, Predicate<Integer> ifNCT) {
        set.removeIf(ifNCT);
    }

}
